package com.yycx.module.bus.provider.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusInvoiceunitService;
import com.yycx.module.bus.provider.service.BusSupplierBankService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import liquibase.pro.packaged.E;
import org.flowable.engine.IdentityService;
import org.flowable.engine.TaskService;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 已办结分页列表扩展器
 *
 * @author zyf
 */
@Component("paymentFinishPageListHandler")
public class PaymentFinishPageListHandler implements PageInterceptor<BusPayment>, ExportInterceptor {
    @Autowired
    private TaskService taskService;

    @Autowired
    private BusSupplierBankService supplierBankService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;
    @Autowired
    private BusSupplierService supplierService;

    @Resource
    private BusCustomService customService;

    @Resource
    private IdentityService identityService;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String amountType=params.get("amountType");
        String payDate1=params.get("payDate1");
        String payDate2=params.get("payDate2");
        cq.select(BusPayment.class, "*");
        //查询部分负责
        cq.addSelect("deptSaleMan.saleManName deptManagerName");
        cq.addSelect(BusSalemanBank.class, "bankName", "bankNo");
        cq.addSelect("saleman.saleManName");
        cq.addSelect("payUser.nickName payUserName");
        cq.addSelect(BusDept.class, "deptName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.like(BusPayment.class, "paymentNo");
        if(FlymeUtils.isNotEmpty(payDate1)) {
            cq.ge(BusPayment.class, "payDate", payDate1);
        }
        if(FlymeUtils.isNotEmpty(payDate2)) {
            cq.le(BusPayment.class, "payDate", payDate2);
        }
        if(FlymeUtils.isEmpty(amountType)) {
            cq.in(true, "amountType", "SupplierBond", "CbReturn", "Invoice", "Settle", "DrugsPlan");
        }else{
            cq.eq("amountType",amountType);
        }
        cq.eq(true, "payment.companyId", OpenHelper.getCompanyId());
        cq.eq(true, "payment.paymentState", params.get("paymentState"));
        cq.createJoin(BusDept.class);
        cq.createJoin(BusSalemanBank.class);
        cq.createJoin(BusSaleman.class).setMainAlias("dept").setMainField("saleManId").setJoinAlias("deptSaleMan").setJoinField("saleManId");
        //通过收款人账户查询收款人姓名
        cq.createJoin(BusSaleman.class).setMainAlias("bsb").setMainField("saleManId").setJoinField("saleManId");
        cq.createJoin(BaseUser.class);
        cq.createJoin(BaseUser.class).setJoinAlias("payUser").setMainField("payUserId");
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            String amountType = entityMap.get("amountType");
            Long customId=entityMap.getLong("customId");
            if(FlymeUtils.isNotEmpty(customId)){
                BusCustom custom=customService.getById(customId);
                if(FlymeUtils.isNotEmpty(custom)){
                    entityMap.put("customName", custom.getCustomName());
                }
            }

            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    if(FlymeUtils.isNotEmpty(task.getAssignee())) {
                        User user = identityService.createUserQuery().userId(task.getAssignee()).singleResult();
                        if (user != null) {
                            entityMap.put("assignee", user.getDisplayName());
                        }
                    }
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
            if (amountType.equals(AmountTypeEnum.Invoice.getCode())) {
                Long invoiceUnitId = entityMap.get("invoiceUnitId");
                BusInvoiceunit invoiceunit=invoiceunitService.getById(invoiceUnitId);
                if(FlymeUtils.isNotEmpty(invoiceunit)){
                    entityMap.put("deptManagerName",invoiceunit.getInvoiceunitName());
                    entityMap.put("saleManName",invoiceunit.getLinkMan());
                }
            }
            if (amountType.equals(AmountTypeEnum.SupplierBond.getCode())) {
                Long supplierId = entityMap.get("supplierId");
                BusSupplier supplier = supplierService.getById(supplierId);
                //收款银行ID
                Long bankId = entityMap.getLong("bankId");
                BusSupplierBank supplierBank = supplierBankService.getById(bankId);
                entityMap.put("deptManagerName", supplier.getSupplierName());
                if(FlymeUtils.isNotEmpty(supplierBank)) {
                    entityMap.put("saleManName", supplierBank.getLinkMan());
                }
            }

        }
        return result;
    }
}
